namespace C1.Win.C1FlexGrid.Util
{
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Globalization;
    using System.Reflection;

    internal class Types
    {
        public static object Coerce(object data, Type type)
        {
            if (((data == null) || (type == null)) || (data.GetType() == type))
            {
                return data;
            }
            try
            {
                return Convert.ChangeType(data, type, null);
            }
            catch
            {
            }
            return null;
        }

        public static double CoerceDouble(object data)
        {
            if (data is string)
            {
                string s = data.ToString();
                if (!IsNumeric(s))
                {
                    return double.NaN;
                }
                return ParseDouble(s);
            }
            try
            {
                switch (Type.GetTypeCode(data.GetType()))
                {
                    case TypeCode.SByte:
                        return (double) ((sbyte) data);

                    case TypeCode.Byte:
                        return (double) ((byte) data);

                    case TypeCode.Int16:
                        return (double) ((short) data);

                    case TypeCode.UInt16:
                        return (double) ((ushort) data);

                    case TypeCode.Int32:
                        return (double) ((int) data);

                    case TypeCode.UInt32:
                        return (double) ((uint) data);

                    case TypeCode.Int64:
                        return (double) ((long) data);

                    case TypeCode.UInt64:
                        return (double) ((ulong) data);

                    case TypeCode.Single:
                        return (double) ((float) data);

                    case TypeCode.Double:
                        return (double) data;

                    case TypeCode.Decimal:
                        return (double) ((decimal) data);
                }
            }
            catch
            {
            }
            return double.NaN;
        }

        public static int Compare(object o1, object o2)
        {
            if (o1 == DBNull.Value)
            {
                o1 = null;
            }
            if (o2 == DBNull.Value)
            {
                o2 = null;
            }
            if ((o1 == null) && (o2 != null))
            {
                return -1;
            }
            if ((o1 != null) && (o2 == null))
            {
                return 1;
            }
            if ((o1 == null) && (o2 == null))
            {
                return 0;
            }
            if (o1.Equals(o2))
            {
                return 0;
            }
            if (o1 == null)
            {
                o1 = string.Empty;
            }
            if (o2 == null)
            {
                o2 = string.Empty;
            }
            try
            {
                if (o1 is IComparable)
                {
                    int num = ((IComparable) o1).CompareTo(o2);
                    return ((num < 0) ? -1 : ((num > 0) ? 1 : 0));
                }
            }
            catch
            {
            }
            return -2;
        }

        public static object FromString(string data, Type type)
        {
            object obj2 = null;
            try
            {
                if (type == typeof(Type))
                {
                    return Type.GetType(data);
                }
                if (type == typeof(Font))
                {
                    data = data.Substring(5, data.Length - 6);
                    string[] strArray = data.Split(new char[] { ',' });
                    return new Font(strArray[0], float.Parse(strArray[1]), (FontStyle) Enum.Parse(typeof(FontStyle), strArray[2], true));
                }
                if (type == typeof(Color))
                {
                    return ColorConverter.FromString(data);
                }
                if (type == typeof(Margins))
                {
                    data = data.Substring(8, data.Length - 9);
                    string[] strArray2 = data.Split(new char[] { ',' });
                    return new Margins(int.Parse(strArray2[0]), int.Parse(strArray2[1]), int.Parse(strArray2[2]), int.Parse(strArray2[3]));
                }
                if (type.IsEnum)
                {
                    return Enum.Parse(type, data, true);
                }
                obj2 = Convert.ChangeType(data, type, null);
            }
            catch
            {
            }
            return obj2;
        }

        public static bool IsNumeric(string s)
        {
            if ((s == null) || (s.Length == 0))
            {
                return false;
            }
            NumberFormatInfo currentInfo = NumberFormatInfo.CurrentInfo;
            string currencySymbol = currentInfo.CurrencySymbol;
            char ch = currentInfo.CurrencyDecimalSeparator[0];
            char ch2 = currentInfo.CurrencyGroupSeparator[0];
            bool flag = true;
            bool flag2 = false;
            bool flag3 = false;
            bool flag4 = false;
            bool flag5 = false;
            bool flag6 = false;
            bool flag7 = false;
            bool flag8 = false;
            bool flag9 = false;
            bool flag10 = false;
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (char.IsDigit(c))
                {
                    if (flag3)
                    {
                        return false;
                    }
                    flag = false;
                    flag2 = flag10 = true;
                }
                else if (c == ch2)
                {
                    if (flag || flag3)
                    {
                        return false;
                    }
                }
                else if (c == ch)
                {
                    if ((flag3 || flag5) || flag9)
                    {
                        return false;
                    }
                    flag5 = true;
                    flag = false;
                }
                else if ((c == '+') || (c == '-'))
                {
                    if ((flag3 || !flag) || flag4)
                    {
                        return false;
                    }
                    flag4 = true;
                }
                else
                {
                    switch (c)
                    {
                        case ' ':
                        case '\t':
                            if (!flag && ((i >= (s.Length - 1)) || (s[i + 1] != currencySymbol[0])))
                            {
                                return false;
                            }
                            break;

                        case ')':
                            if (!flag6 || flag7)
                            {
                                return false;
                            }
                            flag7 = flag3 = true;
                            break;

                        case 'E':
                        case 'e':
                            if (flag || flag3)
                            {
                                return false;
                            }
                            if (flag9)
                            {
                                return false;
                            }
                            flag9 = true;
                            flag10 = false;
                            if ((i < (s.Length - 1)) && ((s[i + 1] == '+') || (s[++i] == '-')))
                            {
                                i++;
                            }
                            break;

                        case '(':
                            if ((flag3 || !flag) || flag4)
                            {
                                return false;
                            }
                            flag4 = flag6 = true;
                            break;

                        default:
                            if ((c == '%') && (i == (s.Length - 1)))
                            {
                                flag3 = true;
                            }
                            else
                            {
                                if (string.Compare(s, i, currencySymbol, 0, currencySymbol.Length) != 0)
                                {
                                    return false;
                                }
                                if (flag8)
                                {
                                    return false;
                                }
                                flag8 = true;
                                if (!flag)
                                {
                                    flag3 = true;
                                }
                                i += currencySymbol.Length - 1;
                            }
                            break;
                    }
                }
            }
            if (!flag2)
            {
                return false;
            }
            if (flag6 != flag7)
            {
                return false;
            }
            if (flag9 && !flag10)
            {
                return false;
            }
            return true;
        }

        public static double ParseDouble(string s)
        {
            if (!IsNumeric(s))
            {
                throw new InvalidCastException("Not a number");
            }
            int index = s.IndexOf('%');
            if (index < 0)
            {
                return double.Parse(s, NumberStyles.Any);
            }
            s = s.Substring(0, index);
            return (double.Parse(s, NumberStyles.Any) / 100.0);
        }

        public static string ToString(object data)
        {
            CultureInfo provider = CultureInfo.InvariantCulture;
            if (data is Font)
            {
                Font font = (Font) data;
                return string.Format(provider, "Font({0},{1},{2})", new object[] { font.Name, font.Size, font.Style });
            }
            if (data is Color)
            {
                Color color = (Color) data;
                return ColorConverter.ToString(color);
            }
            if (data is Margins)
            {
                Margins margins = (Margins) data;
                return string.Format(provider, "Margins({0},{1},{2},{3})", new object[] { margins.Left, margins.Right, margins.Top, margins.Bottom });
            }
            if (data is Enum)
            {
                return data.ToString();
            }
            IConvertible convertible = data as IConvertible;
            if (convertible != null)
            {
                try
                {
                    return convertible.ToString(null);
                }
                catch
                {
                }
            }
            return string.Empty;
        }

        private class ColorConverter
        {
            private static Hashtable _colorTable;

            public static Color FromString(string colorName)
            {
                if (colorName.StartsWith("Color("))
                {
                    colorName = colorName.Substring(6, colorName.Length - 7);
                    string[] strArray = colorName.Split(new char[] { ',' });
                    return Color.FromArgb(int.Parse(strArray[0]), int.Parse(strArray[1]), int.Parse(strArray[2]));
                }
                return GetColor(colorName);
            }

            public static Color GetColor(string colorName)
            {
                Hashtable colorTable = GetColorTable();
                if (colorTable.ContainsKey(colorName))
                {
                    return (Color) colorTable[colorName];
                }
                return Color.White;
            }

            public static string GetColorName(Color color)
            {
                Hashtable colorTable = GetColorTable();
                if (colorTable.ContainsValue(color))
                {
                    foreach (string str in colorTable.Keys)
                    {
                        if (((Color) colorTable[str]) == color)
                        {
                            return str;
                        }
                    }
                }
                return null;
            }

            private static Hashtable GetColorTable()
            {
                if (_colorTable == null)
                {
                    _colorTable = new Hashtable();
                    object[] index = new object[0];
                    foreach (PropertyInfo info in typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static))
                    {
                        _colorTable[info.Name] = info.GetValue(null, index);
                    }
                    foreach (PropertyInfo info2 in typeof(SystemColors).GetProperties(BindingFlags.Public | BindingFlags.Static))
                    {
                        _colorTable[info2.Name] = info2.GetValue(null, index);
                    }
                }
                return _colorTable;
            }

            public static string ToString(Color color)
            {
                string colorName = GetColorName(color);
                if (colorName != null)
                {
                    return colorName;
                }
                return string.Format("Color({0},{0},{0})", color.R, color.G, color.B);
            }
        }
    }
}

